package com.boco.modules.fdoc.service.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.boco.common.constants.BusinessConstants;
import com.boco.common.persistence.Page;
import com.boco.modules.fdoc.dao.FamdoctorDao;
import com.boco.modules.fdoc.dao.ResidentDao;
import com.boco.modules.fdoc.model.FamdoctorEntity;
import com.boco.modules.fdoc.model.QueryResidentEntity;
import com.boco.modules.fdoc.model.ResidentEntity;
import com.boco.modules.fdoc.model.UserDocSignEntity;
import com.boco.modules.fdoc.service.ResidentService;
import com.boco.modules.fdoc.vo.QueryResidentVo;
import com.boco.modules.fdoc.vo.ResidentVo;

@Service
public class ResidentServiceImpl implements ResidentService{
	
	@Resource
	ResidentDao residentDao;
	@Resource
	FamdoctorDao famdocDao;
	@Override
	public int addResident(ResidentEntity entity) {
		ResidentVo vo = residentDao.getByIdCard(entity.getIdCard());
		if (vo != null) { 		//获取到的对象不为空，说明身份证号重复
			return -1;
		}
		entity.setCreateTime(new Date());
		return residentDao.insert(entity);
	}

	@Override
	public int delete(int id) {
		return residentDao.delete(String.valueOf(id));
	}

	@Override
	public int update(ResidentEntity entity) {
		
		entity.setUpdateTime(new Date());
		return residentDao.update(entity);
	}

	@Override
	public ResidentEntity getResident(int id) {
		return residentDao.get(String.valueOf(id));
	}

	@Override
	public List<ResidentVo> getResidenceMember(String idCard) {
		List<ResidentVo> list = residentDao.getResidenceMember(idCard);
		return list;
	}

	@Override
	public ResidentVo getResidentMaster(String idCard) {
		return residentDao.getMasterByIdCard(idCard);
	}
	
	
	@Override
	public Page<QueryResidentEntity> getResidentInfoList(
			Page<QueryResidentEntity> page, QueryResidentEntity entity) {
		entity.setPage(page);
		List<QueryResidentEntity> dataList =residentDao.getResidentInfoList(entity);
		page.setList(dataList);
		return page;
	}
	
	@Override
	public List<ResidentVo> getAllResidentSignSituation(String idCard) {
		return residentDao.getAllResidentSignSituation(idCard);
	}

	@Override
	public ResidentVo getResidentDetail(String idCard) {
		return residentDao.getResidentDetail(idCard);
	}
	
	@Override
	public Page<QueryResidentEntity> getResidentNosigInfoList(
			Page<QueryResidentEntity> page, QueryResidentEntity entity) {
		entity.setPage(page);
		List<QueryResidentEntity> dataList =residentDao.getResidentNosigInfoList(entity);
		page.setList(dataList);
		return page;
	}
	
	@Override
	public List<ResidentVo> getResidentSigninfo(String idCard) {
		return residentDao.getResidentSigninfo(idCard);
	}
	
	@Override
	public int remResisingInfo(UserDocSignEntity userDocSignEntity,boolean flag) {
		if(!flag){
			int remResisingInfo = residentDao.remResisingInfo(userDocSignEntity);
			return remResisingInfo;
		}else{
			int remResisingInfo = residentDao.remResisingInfo(userDocSignEntity);
			if(remResisingInfo > 0){
				List<UserDocSignEntity> singInfobyResiIdCard = residentDao.getSingInfobyResiIdCard(userDocSignEntity);
				//z证明有存在，取只会一个 原则上只有一个记录
				if(singInfobyResiIdCard.size() > 0){
					UserDocSignEntity userDocSignEntity2 = singInfobyResiIdCard.get(0);
					userDocSignEntity2.setStatus(BusinessConstants.SIGN_STATUS_SIGNED); //设置为已签约
					userDocSignEntity2.setSignTime(new Date());
					userDocSignEntity2.setIsNoticed(BusinessConstants.SIGN_NOTICED_NO); //设置为未通知
					Integer sign = famdocDao.sign(userDocSignEntity2);
					return sign;
				}
			}
		}
		return 0;
	}
	
	@Override
	public List<FamdoctorEntity> getAllFamdoctorInfo() {
		return residentDao.getAllFamdoctorInfo();
	}
	
	
	
}
